var Effect = require("Effect");
var FactoryItem = require("FactoryItem");
var SoundManage = require("SoundManage");

cc.Class({
  extends: cc.Component,

  properties: {
    Horizontal: 10,
    Vertical: 10,
    LightUI: cc.Node,
    BoxUI: cc.Node,

    _isHasBox: false,
    _OtherItemList: [],
    _ID: -1,
    _ColorType: -1,
    _isDestory: false,
    _isAlreadyDetection: false,
  },

  Clear() {
    this._OtherItemList.length = 0;
    this._ID = -1;
    this._ColorType = -1;
    this._isDestory = false;
    this._isAlreadyDetection = false;
    this.node.active = false;
    this._isSplice = false;
    this.isTouch = false;
  },

  start() {
    this.isTouch = false;
    //this.ItemList = FactoryItem.Instance.ItemParent.children;
    this.node.on(
      cc.Node.EventType.TOUCH_START,
      () => {
        this.ItemClick();
      },
      this
    );
  },
  /**
   * 用于处理项目（Item）的点击事件
   */
  ItemClick() {
    var self = this;
    // FactoryItem.Instance._IsTouch 是否为 true，表示可以响应触摸事件
    // 检查 FactoryItem.Instance.IsGameStart 是否为 true，表示游戏已经开始
    if (FactoryItem.Instance._IsTouch && FactoryItem.Instance.IsGameStart) {
      // 表示暂时禁用触摸响应
      FactoryItem.Instance._IsTouch = false;
      // 方法停止提示动画
      FactoryItem.Instance.StopPromptAnimation();
      switch (FactoryItem.Instance._TouchState) {
        case "":
          self.TouchNormal(self);
          break;
        case "1":
          self.TouchHammer(self.node);
          break;
        case "2":
          self.TouchChange(self.node);
          break;
        default:
          FactoryItem.Instance._IsTouch = true;
          break;
      }
    }
  },
  /**
   * 处理普通的触摸事件
   */
  TouchNormal(self) {
    var s1 = cc.scaleTo(0.05, 0.8);
    var s2 = cc.scaleTo(0.05, 1);
    var call = cc.callFunc(
      function () {
        self.FindSameItem(true);
      }.bind(self)
    );
    self.node.runAction(cc.sequence(s1, s2, call));
  },

  TouchHammer(item) {
    FactoryItem.Instance.HammerProps(item);
    FactoryItem.Instance._TouchState = "";
    if (FactoryItem.Instance.GameInitCom.PopsList.Hammer > 0) {
      FactoryItem.Instance.UIMianCom.UserHammerProps();
    } else {
      FactoryItem.Instance.UIMianCom.UserDiamond();
    }
    FactoryItem.Instance._IsTouch = true;
  },

  TouchChange(item) {
    FactoryItem.Instance.ChangeProps(item);
    FactoryItem.Instance._IsTouch = true;
  },

  setXY(X, Y) {
    this._X = X;
    this._Y = Y;
  },

  //得到上下左右
  GetLUDR(IshasMyself = true) {
    this.LUDRList = [];

    var _itemList = FactoryItem.Instance.allVerticalList;

    var i = this._X;
    var j = this._Y;

    if (_itemList[i][j] == this.node) {
      if (IshasMyself) {
        this.LUDRList.push(this.node);
      }
      if (i - 1 >= 0) {
        var left = _itemList[i - 1][j];
        if (left != null) this.LUDRList.push(left);
      }
      if (_itemList.length > i + 1) {
        var Right = _itemList[i + 1][j];
        if (Right != null) this.LUDRList.push(Right);
      }
      if (j - 1 >= 0) {
        var Up = _itemList[i][j - 1];
        if (Up != null) this.LUDRList.push(Up);
      }

      if (_itemList[i].length > j + 1) {
        var Down = _itemList[i][j + 1];
        if (Down != null) {
          this.LUDRList.push(Down);
        }
      }
    } else {
      console.log("获取相邻物品失败", _itemList[i][j]);
    }
  },
  /**
   * 查找具有相同颜色类型的物品
   * @param {*} IsMianItem
   * @returns
   */
  FindSameItem(IsMianItem = false) {
    // 检查游戏是否已经开始，如果游戏尚未开始，直接返回，不执行后续逻辑
    if (FactoryItem.Instance.IsGameStart == false) return;
    // 调用 GetLUDR 方法获取当前项目的上下左右相邻项目，并将它们存储在 this.LUDRList 中。
    this.GetLUDR();
    // 遍历 this.LUDRList 中的每一个相邻项目
    for (var i = 0; i < this.LUDRList.length; i++) {
      // 获取相邻项目的 Item 组件
      var ItemCom = this.LUDRList[i].getComponent("Item");
      // 检查相邻项目的颜色类型是否与当前项目相同
      if (ItemCom._ColorType == this._ColorType) {
        // 如果颜色类型相同，调用 FactoryItem.Instance.AddCheckItem 方法将相邻项目添加到检查列表中，并获取当前检查列表的长度
        var count = FactoryItem.Instance.AddCheckItem(this.LUDRList[i]);
        // 如果检查列表的长度大于等于 2，递归调用 ItemCom.FindSameItem 方法继续查找相同的项目。
        if (count >= 2) {
          ItemCom.FindSameItem();
        }
      }
    }
    // 如果 IsMianItem 为 true，表示当前项目是主项目
    if (IsMianItem) {
      // 如果长度大于等于 2，调用 FactoryItem.Instance.ClearCheckItem 方法清除检查列表。
      if (FactoryItem.Instance._CheckItem.length >= 2) {
        FactoryItem.Instance.ClearCheckItem();
      } else {
        // 否则，调用 FactoryItem.Instance.ClearCheckLight 方法清除检查光标
        FactoryItem.Instance.ClearCheckLight();
      }
    }
  },

  //检查相同的Item
  detectionSameItem(IsMianItem = true, isOnlydetection = false) {
    if (FactoryItem.Instance.IsGameStart == false) return;
    this.GetLUDR(false);
    for (var i = 0; i < this.LUDRList.length; i++) {
      var ItemCom = this.LUDRList[i].getComponent("Item");
      if (ItemCom._ColorType == this._ColorType) {
        if (isOnlydetection && this.LUDRList[i] != this.node) {
          return false;
        }
        if (!FactoryItem.Instance.AddMaxItem(this.LUDRList[i])) {
          ItemCom.detectionSameItem(false);
        }
      }
    }
    if (IsMianItem) {
      FactoryItem.Instance.CompareItemList();
    }
  },

  //提示星星的动画
  PromptAnimation() {
    var s1 = cc.scaleTo(0.5, 0.8);
    var s2 = cc.scaleTo(0.5, 1);
    var forever = cc.repeatForever(cc.sequence(s1, s2));
    forever.setTag(1);
    this.node.runAction(forever);
  },
  //停止提示星星的动画
  StopPromptAnimation() {
    this.node.stopActionByTag(1);
    this.node.setScale(1);
  },

  //星星消失动画
  PlayDestoryAnimation(isplayEffect = true) {
    SoundManage.Instance.playDestoryStar();
    if (isplayEffect) {
      Effect.Instance.PlayStarEffect(this.node.getPosition(), this._ColorType);
    }
    this.node.active = false;
  },

  ShowLight() {
    this.LightUI.active = true;
  },
  /**
   * 用于隐藏项目（Item）的高亮效果和盒子效果
   */
  HideLight() {
    // 隐藏高亮效果
    this.LightUI.active = false;
    // 隐藏盒子效果
    this.BoxUI.active = false;
    // 更新盒子状态
    this._isHasBox = false;
  },

  ShowBox() {
    this.BoxUI.active = true;
    this._isHasBox = true;
    if (Utils.isPopRed()) {
      this.BoxUI.children[0].active = false;
      this.BoxUI.children[1].active = true;
    } else {
      this.BoxUI.children[0].active = true;
      this.BoxUI.children[1].active = false;
    }
  },
});
